package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.config.redis.redis.util.Redishandler0;
import cn.stylefeng.guns.core.RecoverConst;
import cn.stylefeng.guns.modular.business.common.Constants;
import cn.stylefeng.guns.modular.business.entity.PageContent;
import cn.stylefeng.guns.modular.business.mapper.PageContentMapper;
import cn.stylefeng.guns.modular.business.model.params.PageContentParam;
import cn.stylefeng.guns.modular.business.model.result.PageContentResult;
import cn.stylefeng.guns.modular.business.service.PageContentService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 单页 服务实现类
 * </p>
 *
 * @author
 * @since 2019-07-24
 */
@Service
public class PageContentServiceImpl extends ServiceImpl<PageContentMapper, PageContent> implements PageContentService {
    @Autowired
    private Redishandler0 redishandler0;

    @Override
    public void add(PageContentParam param) {
        PageContent entity = getEntity(param);
        entity.setDeleteFlag(RecoverConst.DEL_FLAG_FALSE);
        this.save(entity);
        List<String> newList = new ArrayList<>();
        newList.add(JSONObject.toJSONString(entity));
        redishandler0.cacheList(Constants.PAGE_CONTENT_KEY, entity.getPageCode(), newList);
    }

    @Override
    public void delete(PageContentParam param) {
        PageContent content = this.baseMapper.selectById(param.getId());
        content.setDeleteFlag(RecoverConst.DEL_FLAG_TRUE);
        this.updateById(content);
        List<String> list = redishandler0.getList(Constants.PAGE_CONTENT_KEY, content.getPageCode());
        if (!CollectionUtils.isEmpty(list)) {
            PageContent pageContent;
            for (String s : list) {
                pageContent = JSONObject.parseObject(s, PageContent.class);
                if (pageContent.getId().equals(param.getId())) {
                    list.remove(s);
                    break;
                }
            }
            redishandler0.remove(Constants.PAGE_CONTENT_KEY, content.getPageCode());
            redishandler0.cacheList(Constants.PAGE_CONTENT_KEY, content.getPageCode(), list);
        }
    }

    @Override
    public void update(PageContentParam param) {
        PageContent oldEntity = getOldEntity(param);
        PageContent newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
        PageContent content = this.baseMapper.selectById(param.getId());
        List<String> list = redishandler0.getList(Constants.PAGE_CONTENT_KEY, content.getPageCode());
        if (!CollectionUtils.isEmpty(list)) {
            PageContent pageContent;
            for (String s : list) {
                pageContent = JSONObject.parseObject(s, PageContent.class);
                if (pageContent.getId().equals(param.getId())) {
                    list.remove(s);
                    list.add(JSONObject.toJSONString(newEntity));
                    break;
                }
            }
            redishandler0.remove(Constants.PAGE_CONTENT_KEY, content.getPageCode());
            redishandler0.cacheList(Constants.PAGE_CONTENT_KEY, content.getPageCode(), list);
        }
    }

    @Override
    public PageContentResult findBySpec(PageContentParam param) {
        return null;
    }

    @Override
    public List<PageContentResult> findListBySpec(PageContentParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(PageContentParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public List<PageContent> getPageContentByCode(String code) {
        List<String> list = redishandler0.getList(Constants.PAGE_CONTENT_KEY, code);
        List<PageContent> pageContents = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            pageContents = JSONObject.parseArray(list.toString(), PageContent.class);
            pageContents.sort(Comparator.comparing(PageContent::getSort));
        }
        return pageContents;
    }

    private Serializable getKey(PageContentParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }
    private PageContent getOldEntity(PageContentParam param) {
        return this.getById(getKey(param));
    }

    private PageContent getEntity(PageContentParam param) {
        PageContent entity = new PageContent();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
